package com.tang.common.utils;


import com.alibaba.nacos.common.utils.RandomUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;


public class UpLoad {
    public static List<String> upload( List<MultipartFile> files) {
        List<String> list = new ArrayList<>();

        for (MultipartFile file : files) {
            list.add(upload(file));
        }
        return list;
    }
    public static String upload(MultipartFile file) {
        if (file.isEmpty()) {
            return "file is Empty";
        }
        System.out.println(file.getContentType());
        String filename = file.getOriginalFilename();
        try {
            File path = getUploadDirectory();
            String newName = System.currentTimeMillis() + RandomUtils.nextInt(10000,99999)+filename;
            File dest = new File(path, newName);
            System.out.println(dest.getPath());
            file.transferTo(dest);
            return new File("/StaticSource/recruit", newName).toString();
        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        }
    }
    public static File getUploadDirectory() throws FileNotFoundException {
//        String pathName = ResourceUtils.getURL("classpath:").getPath();
//        File path = new File(pathName, "/public/upload");
            File path= new File("E:\\StaticSource\\recruit");
        if (!path.exists()) {
            path.mkdirs();
        }
        System.out.println(path);
        return path;
    }
}
